package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page17Model;
import com.kaizzi.data.jdl.service.CrmMemberService;
import lombok.Data;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/17
 */
@Component
public class PageData17 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){

        List<String> thisMonth = DateUtil.getMonthDate(0);
        List<String> lastMonth = DateUtil.getMonthDate(1);

        //累计会员用户数
        long totalUser = getTotalUser(thisMonth);
        long totalUser1 = getTotalUser(lastMonth);
        //当月新增会员数
        long monthNewUser = getMonthNewUser(thisMonth);
        long monthNewUser1 = getMonthNewUser(lastMonth);
        //当月激活的当月注册的新用户
        long registUser2 = getRegistUser2(thisMonth);
        long registUser21 = getRegistUser2(lastMonth);
        //当月召回老用户
        long oldUser2 = getOldUser2(thisMonth);
        long oldUser21 = getOldUser2(lastMonth);
        //utc
        long utcNewUser = getUtcNewUser(thisMonth);
        long utcNewUser1 = getUtcNewUser(lastMonth);
        //总激活用户
        long thisMonthActiveUser = registUser2+oldUser2;
        long lastMonthActiveUser = registUser21+oldUser21;
        //总激活率
        String thisMonthTotalRate = StringUtil.decimalFormat((double)thisMonthActiveUser/(double) totalUser)+"%";
        String lastMonthTotalRate = StringUtil.decimalFormat((double)lastMonthActiveUser/(double)totalUser1)+"%";
        //一键组队-拉新人数
        long teamNewUser2 = getTeamNewUser2(thisMonth);
        long teamNewUser21 = getTeamNewUser2(lastMonth);
        //一键组队-召回老用户数
        long teamOldUser2 = getTeamOldUser2(thisMonth);
        long teamOldUser21 = getTeamOldUser2(lastMonth);

        List<TempPage17> tempPage17s = new ArrayList<>();
        List<Page17Model> page17s = model.getPage17s();
        for (Page17Model pageModel : page17s) {

//            TempData17 lastKpi = new TempData17();
//            TempData17 thisKpi = new TempData17();
//            TempPage17 tempPage17 = new TempPage17();

            if (pageModel.getName().trim().equals("拉新人数（当月新注册并参与一键组队的用户）")){

                String id = "一键组队";
                String name = "拉新人数（当月新注册并参与一键组队的用户）";
                tempPage17s.add( getTempPage17(pageModel, teamNewUser21, teamNewUser2, id, name));
                continue;
            }
            if (pageModel.getName().trim().equals("召回老用户数（注册时间小于当月且在当月有一键组队行为）")){
                String id = "";
                String name = "召回老用户数（注册时间小于当月且在当月有一键组队行为）";
                tempPage17s.add( getTempPage17(pageModel, teamOldUser21, teamOldUser2, id, name));
                continue;
            }
            if (pageModel.getName().trim().equals("激活率*")){
                String id ="";
                String name = "激活率*";
                String lastKpi = pageModel.getLastKpi();
                String thisKpi = pageModel.getThisKpi();

                TempPage17 tempPage17 = new TempPage17();
                tempPage17.setId(id);
                tempPage17.setName(name);

                TempData17 lastData = new TempData17();
                TempData17 thisData = new TempData17();

                String thisRate = StringUtil.decimalFormat((double) (teamNewUser2 + teamOldUser2) / (double) totalUser*100)+"%";
                String lastRate = StringUtil.decimalFormat((double) (teamNewUser21 + teamOldUser21) / (double) totalUser1*100) +"%";

                lastData.setRate("/");
                lastData.setRealKpi(lastRate);
                lastData.setTargetKpi(lastKpi);

                thisData.setRate("/");
                thisData.setRealKpi(thisRate);
                thisData.setTargetKpi(thisKpi);

                tempPage17.setLastKpi(lastData);
                tempPage17.setThisKpi(thisData);

                tempPage17s.add(tempPage17);
//                getTempPage17(pageModel,)
                continue;
            }
            if (pageModel.getId().equals("a")){
                String id = "a";
                String name = "累计会员用户数";
                tempPage17s.add(getTempPage17(pageModel,totalUser1,totalUser,id,name));
                continue;
            }
            if (pageModel.getId().equals("b")){
                String id = "b";
                String name = "当月新增会员数";
                tempPage17s.add(getTempPage17(pageModel,monthNewUser1,monthNewUser,id,name));
                continue;
            }
            if (pageModel.getId().equals("c")){
                String id = "c";
                String name = "当月激活的当月注册的新用户";
                tempPage17s.add(getTempPage17(pageModel,registUser21,registUser2,id,name));
                continue;
            }
            if (pageModel.getId().equals("d")){
                String id = "d";
                String name = "当月召回老用户";
                tempPage17s.add(getTempPage17(pageModel,oldUser21,oldUser2,id,name));
                continue;
            }
            if (pageModel.getId().equals("e")){
                String id = "e";
                String name = "当月激活用户(e=c+d)";
                tempPage17s.add(getTempPage17(pageModel,lastMonthActiveUser,thisMonthActiveUser,id,name));
                continue;
            }
            if (pageModel.getId().equals("f")){
                String id = "f";
                String name = "当月用户总激活率（f=e/a)";

                //目标kpi
                String lastKpi = pageModel.getLastKpi();
                String thisKpi = pageModel.getThisKpi();
                //激活率
                String activeRate1 = StringUtil.decimalFormat((double) lastMonthActiveUser/(double)totalUser1*100)+"%";
                String activeRate = StringUtil.decimalFormat((double) thisMonthActiveUser/(double)totalUser*100)+"%";
                TempPage17 tempPage17 = new TempPage17();
                tempPage17.setName(name);
                tempPage17.setId(id);

                TempData17 lastData = new TempData17();
                lastData.setRate("/");
                lastData.setRealKpi(activeRate1);
                lastData.setTargetKpi(lastKpi);

                TempData17 thisData = new TempData17();
                thisData.setRate("/");
                thisData.setRealKpi(activeRate);
                thisData.setTargetKpi(thisKpi);

                tempPage17.setLastKpi(lastData);
                tempPage17.setThisKpi(thisData);
                tempPage17s.add(tempPage17);
                continue;
            }
            if (pageModel.getId().equals("UTC")){
                String id = "UTC";
                String name = "UTC渠道新增用户数";
                tempPage17s.add(getTempPage17(pageModel,utcNewUser1,utcNewUser,id,name));
//                continue;
            }
        }

        Page17 page17 = new Page17();
        page17.setTitle(LogicUtil.getTitle(model,17));
        page17.setSummary(getSummary(LogicUtil.getContent(model,17,"Summary")));
        page17.setActionPlan(getActionPlan(LogicUtil.getContent(model,17,"Action Plan")));
        page17.setTitle(LogicUtil.getTitle(model,17));

        page17.setDataPeriod(DateUtil.getLastWeekDate());
        page17.setPage17s(tempPage17s);

        return JsonUtil.serialize(page17);
    }

    private List<String> getActionPlan(String actionPlan) {
        String[] split = actionPlan.split("\n");
        List<String> actions = new ArrayList<>();
        for (String s : split) {
            if (s.equals("")){
                continue;
            }
            actions.add(s);
        }
        return actions;
    }

    private List<String> getSummary(String summary) {
        String[] split = summary.split("\n");
        List<String> summarys = new ArrayList<>();
        for (String s : split) {
            if (s.equals("")){
                continue;
            }
            summarys.add(s);
        }
        return summarys;
    }

    private TempPage17 getTempPage17(Page17Model pageModel,long lastKpiCount,long thisKpiCount,String id,String name){
        TempPage17 tempPage17 = new TempPage17();
        TempData17 lastKpi = new TempData17();
        TempData17 thisKpi = new TempData17();

        String realKpiRate = getRealKpiRate(pageModel.getThisKpi(), thisKpiCount);
        String realKpiRate1 = getRealKpiRate(pageModel.getLastKpi(), lastKpiCount);

        lastKpi.setRealKpi(StringUtil.pointFormat(lastKpiCount));
        lastKpi.setTargetKpi(pageModel.getLastKpi());
        lastKpi.setRate(realKpiRate1);

        thisKpi.setRealKpi(StringUtil.pointFormat(thisKpiCount));
        thisKpi.setTargetKpi(StringUtil.pointFormat(Double.parseDouble(pageModel.getThisKpi().replaceAll(",",""))));
        thisKpi.setRate(realKpiRate);

        tempPage17.setLastKpi(lastKpi);
        tempPage17.setThisKpi(thisKpi);
        tempPage17.setId(id);
        tempPage17.setName(name);

        return tempPage17;
    }

    private String getRealKpiRate(String excel,long mySql){
        try {
            return StringUtil.decimalFormat(mySql / Double.parseDouble(excel.replaceAll(",", "")) * 100) +"%";
        }catch (Exception e){
            return "0%";
        }
    }

    //累计会员用户数
    private long getTotalUser(List<String> list){
        return crmMemberService.getUserCount("",list.get(1),"");
    }

    //当月新增会员数
    private long getMonthNewUser(List<String> list){
        return crmMemberService.getUserCount(list.get(0),list.get(1),"");
    }

    //utc渠道新增用户数
    private long getUtcNewUser(List<String> list){
        return crmMemberService.getUserCount(list.get(0),list.get(1),"utc");
    }

    //当月激活的当月注册的新用户
    private long getRegistUser2(List<String> list){
        return crmMemberService.getRegistUser2(list.get(0), list.get(1));
    }

    //当月召回老用户
    private long getOldUser2(List<String> list){
        return crmMemberService.getOldUser2(list.get(0),list.get(1));
    }

    //一键组队-拉新人数
    private long getTeamNewUser2(List<String> list){
        return crmMemberService.getTeamNewUser2(list.get(0),list.get(1));
    }

    //一键组队-召回老用户数
    private long getTeamOldUser2(List<String> list){
        return crmMemberService.getTeamOldUser2(list.get(0),list.get(1));
    }

    @Data
    private static class Page17{
        private String dataPeriod;
        private String title;
        private List<TempPage17> page17s;
        private List<String> summary;
        private List<String> actionPlan;
    }

    @Data
    private static class TempPage17{
        private String id;
        private String name;
        private TempData17 lastKpi;
        private TempData17 thisKpi;

    }

    @Data
    private static class TempData17{
        private String targetKpi;
        private String realKpi;
        private String rate;
    }

}
